package com.springboot.controller;

import com.springboot.util.properties.SecurityProperties;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 * @Author: TongRui乀
 * @Date: 2019/6/21 15:42
 * @description：
 */
@Controller
@RequestMapping("/web")
public class WebController {

    private Logger logger = LoggerFactory.getLogger(WebController.class);

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("page")
    public String page(){
        return "test";
    }

    @Autowired
    private SecurityProperties securityProperties;

    @GetMapping("test")
    @ResponseBody
    public String test(){

        RLock rLock = redissonClient.getLock("test");

        try {
            boolean b = rLock.tryLock(5, 30, TimeUnit.SECONDS);

            if(b){
                logger.info("当前线程：{} 获取锁成功", Thread.currentThread().getName());
                Thread.sleep(20000);

                rLock.unlock();
            }else{
                logger.info("当前线程：{} 获取锁失败", Thread.currentThread().getName());
                return "Locked";
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return "SUCCESS";
    }


    @ResponseBody
    @GetMapping("properties")
    public String getProperties(){
        return securityProperties.getCode().getImage().getPath();
    }

}
